home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htsserver.h < prev    next >
Text File  |  2006-10-15  |  8KB  |  302 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Mini-server                                            */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. // Fichier intercepteur d'URL .h
  38.  
  39. #ifndef HTS_SERVER_DEFH
  40. #define HTS_SERVER_DEFH 
  41.  
  42. #include "htsnet.h"
  43.  
  44. /* String */
  45. #include "htsstrings.h"
  46.  
  47.  
  48. // Fonctions
  49. void socinput(T_SOC soc,char* s,int max);
  50. T_SOC smallserver_init_std(int* port_prox,char* adr_prox,int defaultPort);
  51. T_SOC smallserver_init(int* port,char* adr);
  52. int smallserver(T_SOC soc,char* url,char* method,char* data, char* path);
  53.  
  54. #define CATCH_RESPONSE \
  55.   "HTTP/1.0 200 OK\r\n"\
  56.   "Content-type: text/html\r\n"\
  57.   "\r\n"\
  58.   "<!-- Generated by HTTrack Website Copier -->\r\n"\
  59.   "<HTML><HEAD>\r\n"\
  60.   "<TITLE>Link caught!</TITLE>\r\n"\
  61.   "<SCRIPT LANGUAGE=\"Javascript\">\r\n"\
  62.   "<!--\r\n"\
  63.   "function back() {\r\n"\
  64.   "  history.go(-1);\r\n"\
  65.   "}\r\n"\
  66.   "// -->\r\n"\
  67.   "</SCRIPT>\r\n"\
  68.   "</HEAD>\r\n"\
  69.   "<BODY>\r\n"\
  70.   "<H2>Link captured into HTTrack Website Copier, you can now restore your proxy preferences!</H2>\r\n"\
  71.   "<BR><BR>\r\n"\
  72.   "<H3><A HREF=\"javascript:back();\">Clic here to go back</A></H3>\r\n"\
  73.   "</BODY></HTML>"\
  74.   "<!-- Generated by HTTrack Website Copier -->\r\n"\
  75.   "\r\n"\
  76.  
  77. extern int NewLangStrSz;
  78. extern inthash NewLangStr;
  79. extern int NewLangStrKeysSz;
  80. extern inthash NewLangStrKeys;
  81. extern int NewLangListSz;
  82. extern inthash NewLangList;
  83.  
  84. extern httrackp *global_opt;
  85.  
  86. /* Spaces: CR,LF,TAB,FF */
  87. #define  is_space(c)      ( ((c)==' ') || ((c)=='\"') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11) || ((c)=='\'') )
  88. #define  is_realspace(c)  ( ((c)==' ')                || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11)                )
  89. #define  is_taborspace(c) ( ((c)==' ')                                          || ((c)==9)                             )
  90. #define  is_quote(c)      (               ((c)=='\"')                                                    || ((c)=='\'') )
  91. #define  is_retorsep(c)   (                              ((c)==10) || ((c)==13) || ((c)==9)                                          )
  92.  
  93. extern int smallserver_setkey(char* key, char* value);
  94. extern int smallserver_setkeyint(char* key, LLint value);
  95. extern int smallserver_setkeyarr(char* key, int id, char* key2, char* value);
  96.  
  97.  
  98. /* Language files */
  99. static int htslang_load(char* limit_to, char* apppath);
  100. static void conv_printf(char* from,char* to);
  101. static void LANG_DELETE(void);
  102. static void LANG_INIT(char* path);
  103. static int LANG_T(char* path, int l);
  104. static int QLANG_T(int l);
  105. static char* LANGSEL(char* name);
  106. static char* LANGINTKEY(char* name);
  107. static int LANG_SEARCH(char* path, char* iso);
  108. static int LANG_LIST(char* path, char* buffer);
  109.  
  110. int htslang_init(void);
  111. int htslang_uninit(void);
  112.  
  113. /* Static definitions */
  114.  
  115. static char* gethomedir(void);
  116. static int linput_cpp(FILE* fp,char* s,int max);
  117. static int linput_trim(FILE* fp,char* s,int max);
  118. static int fexist(const char* s);
  119. static int linput(FILE* fp,char* s,int max);
  120.  
  121. static int linputsoc(T_SOC soc, char* s, int max) {
  122.   int c;
  123.   int j=0;
  124.   do {
  125.     unsigned char ch;
  126.     if (recv(soc, &ch, 1, 0) == 1) {
  127.       c = ch;
  128.     } else {
  129.       c = EOF;
  130.     }
  131.     if (c!=EOF) {
  132.       switch(c) {
  133.         case 13: break;  // sauter CR
  134.         case 10: c=-1; break;
  135.         case 9: case 12: break;  // sauter ces caractΦres
  136.         default: s[j++]=(char) c; break;
  137.       }
  138.     }
  139.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  140.   s[j]='\0';
  141.   return j;
  142. }
  143.  
  144. static int check_readinput_t(T_SOC soc, int timeout) {
  145.   if (soc != INVALID_SOCKET) {
  146.     fd_set fds;           // poll structures
  147.     struct timeval tv;          // structure for select
  148.     FD_ZERO(&fds);
  149.     FD_SET(soc,&fds);           
  150.     tv.tv_sec=timeout;
  151.     tv.tv_usec=0;
  152.     select(soc + 1,&fds,NULL,NULL,&tv);
  153.     if (FD_ISSET(soc,&fds))
  154.       return 1;
  155.     else
  156.       return 0;
  157.   } else
  158.     return 0;
  159. }
  160.  
  161. static int linputsoc_t(T_SOC soc, char* s, int max, int timeout) {
  162.   if (check_readinput_t(soc, timeout)) {
  163.     return linputsoc(soc, s, max);
  164.   }
  165.   return -1;
  166. }
  167.  
  168. static char* gethomedir(void) {
  169.   char* home = getenv( "HOME" );
  170.   if (home)
  171.     return home;
  172.   else
  173.     return ".";
  174. }
  175. static int linput_cpp(FILE* fp,char* s,int max) {
  176.   int rlen=0;
  177.   s[0]='\0';
  178.   do {
  179.     int ret;
  180.     if (rlen>0)
  181.     if (s[rlen-1]=='\\')
  182.       s[--rlen]='\0';      // couper \ final
  183.     // lire ligne
  184.     ret=linput_trim(fp,s+rlen,max-rlen);
  185.     if (ret>0)
  186.       rlen+=ret;
  187.   } while((s[max(rlen-1,0)]=='\\') && (rlen<max));
  188.   return rlen;
  189. }
  190.  
  191. static int fexist(const char* s) {
  192.   struct stat st;
  193.   memset(&st, 0, sizeof(st));
  194.   if (stat(s, &st) == 0) {
  195.     if (S_ISREG(st.st_mode)) {
  196.       return 1;
  197.     }
  198.   }
  199.   return 0;
  200. static int linput(FILE* fp,char* s,int max) {
  201.   int c;
  202.   int j=0;
  203.   do {
  204.     c=fgetc(fp);
  205.     if (c!=EOF) {
  206.       switch(c) {
  207.         case 13: break;  // sauter CR
  208.         case 10: c=-1; break;
  209.         case 0: case 9: case 12: break;  // sauter ces caractΦres
  210.         default: s[j++]=(char) c; break;
  211.       }
  212.     }
  213.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  214.   s[j]='\0';
  215.   return j;
  216. }
  217. static int linput_trim(FILE* fp,char* s,int max) {
  218.   int rlen=0;
  219.   char* ls=(char*) malloct(max+2);
  220.   s[0]='\0';
  221.   if (ls) {
  222.     char* a;
  223.     // lire ligne
  224.     rlen=linput(fp,ls,max);
  225.     if (rlen) {
  226.       // sauter espaces et tabs en fin
  227.       while( (rlen>0) && is_realspace(ls[max(rlen-1,0)]) )
  228.         ls[--rlen]='\0';
  229.       // sauter espaces en dΘbut
  230.       a=ls;
  231.       while((rlen>0) && ((*a==' ') || (*a=='\t'))) {
  232.         a++;
  233.         rlen--;
  234.       }
  235.       if (rlen>0) {
  236.         memcpy(s,a,rlen);      // can copy \0 chars
  237.         s[rlen]='\0';
  238.       }
  239.     }
  240.     //
  241.     freet(ls);
  242.   }
  243.   return rlen;
  244. }
  245.  
  246. static int ehexh(char c) {
  247.   if ((c>='0') && (c<='9')) return c-'0';
  248.   if ((c>='a') && (c<='f')) c-=('a'-'A');
  249.   if ((c>='A') && (c<='F')) return (c-'A'+10);
  250.   return 0;
  251. }
  252.  
  253. static int ehex(char* s) {
  254.   return 16*ehexh(*s)+ehexh(*(s+1));
  255. }
  256.  
  257. static void unescapehttp(char* s, String* tempo) {
  258.   int i;
  259.   for (i=0;i<(int) strlen(s);i++) {
  260.     if (s[i]=='%' && s[i+1]=='%') {
  261.       i++;
  262.       StringAddchar(*tempo, '%');
  263.     } else if (s[i]=='%') {
  264.       char hc;
  265.       i++;
  266.       hc = (char) ehex(s+i);
  267.       StringAddchar(*tempo, (char) hc);
  268.       i++;    // sauter 2 caractΦres finalement
  269.     }
  270.     else if (s[i]=='+') {
  271.       StringAddchar(*tempo, ' ');
  272.     }
  273.     else
  274.       StringAddchar(*tempo, s[i]);
  275.   }
  276. }
  277.  
  278. static void unescapeini(char* s, String* tempo) {
  279.   int i;
  280.   char lastc=0;
  281.   for (i=0;i<(int) strlen(s);i++) {
  282.     if (s[i]=='%' && s[i+1]=='%') {
  283.       i++;
  284.       StringAddchar(*tempo, lastc = '%');
  285.     } else if (s[i]=='%') {
  286.       char hc;
  287.       i++;
  288.       hc = (char) ehex(s+i);
  289.       if (!is_retorsep(hc) || !is_retorsep(lastc)) {
  290.         StringAddchar(*tempo, lastc = (char) hc);
  291.       }
  292.       i++;    // sauter 2 caractΦres finalement
  293.     }
  294.     else
  295.       StringAddchar(*tempo, lastc = s[i]);
  296.   }
  297. }
  298.  
  299. #endif
  300.  
  301.